package com.witframework.web.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.witframework.util.HtmlBuilder;


public class BindErrors extends TagSupport{
	private String style="COLOR:RED;font-size:10pt;font-weight:bold;";
	private String styleClass=null;
	@Override
	public int doStartTag() throws JspException {			
		HtmlBuilder html=new HtmlBuilder();
		html=html.span().id("errors");
		if(styleClass!=null)
			html=html.styleClass(styleClass);
		else			
			html=html.style(style);
		html.close();
		List fieldErrors = (List) pageContext.getRequest().getAttribute("fieldErrors");       
        if (fieldErrors != null)
        for(int i=0;i<fieldErrors.size();i++){
    		if(i>=1) html.append("</BR>");
    		html.append(fieldErrors.get(i));			
    	}
        pageContext.removeAttribute("fieldErrors");
		
		html.spanEnd();
		JspWriter writer = pageContext.getOut();
		try {			
			writer.println(html.toString());
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}	
	
}
